package com.project.slsx.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.project.slsx.Constant.Constans;
import com.project.slsx.R;
import com.project.slsx.adapter.GuadanAdapter;
import com.project.slsx.bean.GoodsBean;
import com.project.slsx.bean.GuaDanBean;
import com.project.slsx.utils.SPUtils;

import java.util.List;

/**
 * 挂单详情
 */

public class GuadanDetailActivity extends BaseActivity {
    private static final String TAG = GuadanDetailActivity.class.getSimpleName();
    private GuadanAdapter adapter;
    private List<List<GoodsBean>> guadanList;

    @Override
    public int getLayoutId() {
        return R.layout.activity_guadan_detial;
    }

    @Override
    public void initView() {
        TextView title = findViewById(R.id.tv_title);
        title.setText("挂单");
        findViewById(R.id.rl_back).setOnClickListener(v -> finish());
        RecyclerView recyclerview = findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        adapter = new GuadanAdapter(mContext, R.layout.layout_item_guadan);
        recyclerview.setAdapter(adapter);
    }

    @Override
    public void initData() {
        String string = SPUtils.getInstance().getString(Constans.GUADAN, "");
        if (!TextUtils.isEmpty(string)) {
            GuaDanBean guaDanBean = JSONObject.parseObject(string, GuaDanBean.class);
            guadanList = guaDanBean.getGuadanList();
            if (guadanList.size() > 0) {
                adapter.setNewData(guadanList);
            }
        } else {
            showToast(mContext, "无挂单数据");
        }

        adapter.setOnDeleteClickListener(position -> {
            Log.i(TAG, "initData: " + position);
            adapter.remove(position);

            String string1 = SPUtils.getInstance().getString(Constans.GUADAN, "");
            GuaDanBean guaDanBean = JSONObject.parseObject(string1, GuaDanBean.class);
            List<List<GoodsBean>> guadanList = guaDanBean.getGuadanList();
            guadanList.remove(position);

            String s = JSONObject.toJSONString(guaDanBean);
            SPUtils.getInstance().put(Constans.GUADAN, s);
        });

        adapter.setOnItemClickListener((adapter, view, position) -> {
            List<GoodsBean> result = (List<GoodsBean>) adapter.getData().get(position);
            Log.i(TAG, "initData: " + position + "result :" + result);
            Intent intent = new Intent();
            intent.putExtra(Constans.RESULT, position);
            setResult(RESULT_OK, intent);
            finish();
        });
    }
}
